<template>
	<view>
		<u-form :model="form" ref="uForm">
			<u-form-item label="问题">
				<u-input v-model="QuestionVo.question.description" />
			</u-form-item>
			<u-upload ref="uUpload" :action="action" :auto-upload="false" @on-success="success"></u-upload>
			<!-- <u-button @click="loadImage">上传图片</u-button> -->
			<u-button type="primary" @click="submit">提交问题</u-button>
		</u-form>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				QuestionVo: {
					question: {
						description: "",
					},
					page: "",
					imageUrl: ""
				},
				form: {

				},
				action: 'http://43.136.183.146:9090/upload/image',
				showUploadList: false,
				// 这里直接获取内部的lists变量即可
				lists: [],
			};
		},
		// 只有onReady生命周期才能调用refs操作组件
		onReady() {
			// 得到整个组件对象，内部图片列表变量为"lists"
			this.lists = this.$refs.uUpload.lists;
		},
		methods: {
			onPageto(url) {
				uni.navigateTo({
					url: url
				})
			},
			showToast() {
							this.$refs.uToast.show({
								title: '提交成功',
								type: 'success',
								url: '/pages/life/addQuestion'
							})
			},
			
			success(data, index, lists, name) {
				console.log(data[0]);
				this.QuestionVo.imageUrl = data[0];
			},
			submit() {
				this.$refs.uUpload.upload();
				console.log(this.QuestionVo.question.description);
				if(this.QuestionVo.question.description.length<15){
					this.$refs.uToast.show({
						title: '问题字数不低于15',
						type: 'warning',
					})
					return;
				}
						
				this.$axios.post("/api/life/question/add",this.QuestionVo).then(r => {
						if(r.data.code==200){
							this.showToast();	
						}else{
							this.$refs.uToast.show({
								title: '出错啦！',
								type: 'error',
							})
						}
					});
				}
			}
		};
</script>
